package org.realteam.hibernate.test.util;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.realteam.hibernate.test.model.inheritance.Employee;

public class EventManager {
	
	public static void main(String[] args) {
		EventManager eventManager = new EventManager();
		
//		Event event = new Event();
//		event.setTitle("My Event3");
//		event.setDate(new Date());
//		event.setGender(GenderEnum.OTHER);
		
//		Message message = new Message();
//		message.setText("My first message");
//		
//		Message nextMsg = new Message();
//		nextMsg.setText("My next message");
//		 
//		message.setNextMessage(nextMsg);
		
//		JessCat jess = new JessCat();
//		jess.setName("Jess");
//		jess.setJessSex(Gender.FEMALE);
//		jess.setJessAge(1);
		
//		TomCat tom = new TomCat();
//		tom.setName("Stranger");
//		tom.setTomSex(Gender.OTHER);
//		tom.setTomAge(3);
		
//		Cat cat = new Cat();
//		cat.setName("asdfsdf");
//		
//		eventManager.createAndStoreEvent(cat);
		
		List<Object> list = eventManager.findEvents(Employee.class);
		
		for (Iterator iter = list.iterator(); iter.hasNext();) {
			Employee event = (Employee) iter.next();
			System.out.println(event.getEmpName() + "----" + event.getPayScale().getName() + "----" + event.getPayScale().getName());
		}
		
//		Student st = new Student();
//		
//		st.setName("丁鹏");
//		st.setAge(24);
//		st.setGender(Gender.MALE);
//		st.setGrade(9);
//		st.setHobbies("计算机");
		
//		Teacher t = new Teacher();
//		
//		t.setName("汤姆");
//		t.setAge(50);
//		t.setGender(Gender.FEMALE);
//		t.setJob("Teaching");
//		t.setSubject("literature and language");
		
//		eventManager.createAndStoreEvent(t);
		
//		Student st = (Student)eventManager.fechObject(Long.valueOf(1), Student.class);
//		System.out.println(st.getName() + "---" + st.getAge() + "---" + st.getHobbies());
		
//		Employee emp = new Employee();
//		emp.setPrimaryKey(new EmployeePK(KeyGenerater.PREFIX, KeyGenerater.getLongTime()));
//		emp.setStatus(EmployeeStatus.FULL_TIME);
//		emp.setPayScale(SalaryRate.MANAGER);
//		emp.setEmpName("肖敬若");
//		emp.setEmpSalary(999.3f);
//		emp.setEmpClass(342);
		
//		eventManager.createAndStoreEvent(emp);
	}
	
	public List<Object> findEvents(Class clazz) {
		Session session = HibernateUtil.getSessionFacotry().getCurrentSession();

		session.beginTransaction();
		Query query = session.createQuery("from " + clazz.getName());
		List<Object> objs = query.list();
		session.close();
		
		return objs;
	}
	
	public void createAndStoreEvent(Object entity) {
		Session session = HibernateUtil.getSessionFacotry().getCurrentSession();
		session.beginTransaction();
		session.save(entity);
		session.getTransaction().commit();
		HibernateUtil.getSessionFacotry().close();
	}
	
	public void updateObject(Object entity) {
		Session session = HibernateUtil.getSessionFacotry().getCurrentSession();
		session.beginTransaction();
		session.update(entity);
		session.getTransaction().commit();
		//HibernateUtil.getSessionFacotry().close();
	}
	
	public Object fechObject(Serializable id, Class clazz) {
		Session session = HibernateUtil.getSessionFacotry().getCurrentSession();
		session.beginTransaction();
		Object result = session.get(clazz, id);
		session.getTransaction().commit();
		HibernateUtil.getSessionFacotry().close();
		return result;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
